<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp" maxlength="11" placeholder="请输入手机号码" type="text" v-model="mobile">
        </div>
        <div class="form-item">
          <input class="inp" maxlength="5" placeholder="请输入图形验证码" type="text" v-model="PicCode">
          <img :src="PicUrl" alt="" @click="getPicCode" v-if="PicUrl">
        </div>
        <div class="form-item">
          <input class="inp" placeholder="请输入短信验证码" type="text" v-model="msgCode">
          <button @click="getCode">{{totalTime===secondTime?'获取验证码':`${secondTime}s后获取`}}</button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<script>
import { getPicCode, getMsgCode, codeLogin } from '@/apis/login'
import { Toast } from 'vant'
// import '@/store/modules/user'

export default {
  name: 'LoginPage',
  data () {
    return {
      PicCode: '', // 用户输入的图形验证码
      mobile: '', // 手机号
      PicKey: '', // 图形验证码的唯一标识
      PicUrl: '', // 存储请求渲染的图片地址
      totalTime: 60, // 总时间
      secondTime: 60, // 递减的时间
      timer: null, // 定时器id
      msgCode: ''// 用户输入短信验证码
    }
  },
  created () {
    this.getPicCode()
  },
  methods: {
    async  getPicCode () {
      const { data: { base64, key } } = await getPicCode()
      this.PicKey = key
      this.PicUrl = base64
    },
    // 获取短信验证码
    async getCode () {
      if (this.validFn() === false) return
      if (!this.timer && this.totalTime === this.secondTime) {
        const res = await getMsgCode(this.PicCode, this.PicKey, this.mobile)
        console.log(res)
        Toast('短信验证码发送成功!')
        this.timer = setInterval(() => {
          this.secondTime--
          if (this.secondTime === 0) {
            this.secondTime = this.totalTime
            clearInterval(this.timer)
            this.timer = null
          }
        }, 1000)
      }
    },
    // 判断手机号，图像验证码格式
    validFn () {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        Toast('请输入正确的手机号!')
        return false
      }
      if (!/^\w{4}$/.test(this.PicCode)) {
        Toast('请输入正确的图形验证码!')
        return false
      }
      return true
    },
    // 登录
    async login () {
      if (!this.validFn()) return
      if (!/^\d{6}$/.test(this.msgCode)) {
        Toast('请输入正确的短信验证码')
        return
      }
      const res = await codeLogin({
        isParty: false,
        mobile: this.mobile,
        partyData: {},
        smsCode: this.msgCode
      })
      console.log(res)
      Toast('登陆成功！')
      this.$store.commit('user/setUserInfo', { token: res.data.token, userId: res.data.userId })
      const url = this.$route.query.backURL || '/'
      this.$router.replace({ path: url })
    }
  },
  // 离开页面时销毁定时器
  destroyed () {
    this.secondTime = this.totalTime
    clearInterval(this.timer)
    this.timer = null
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
